/**
 * Copyright 2012 Brigham Young University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.byu.nlp.cluster.em;

import edu.byu.nlp.data.SparseFeatureVector;
import edu.byu.nlp.util.DoubleArrays;
import edu.byu.nlp.util.Matrices;

/**
 * Class for incrementally computing partial counts. Note that the count arrays are re-used when reset is called.
 * It is therefore advisable to immediately clone (and deep clone) the count arrays.
 * 
 * @author rah67
 *
 */
public class PartialCounts implements Cloneable {

	// The following state will be updated incrementally between calls to visitPosterior
	private final double[] yCounts;
	private final double[][] xGivenYCounts;
	
	private PartialCounts(double[] yCounts, double[][] xGivenYCounts) {
		this.yCounts = yCounts;
		this.xGivenYCounts = xGivenYCounts;
	}

	public void incrementCounts(SparseFeatureVector instance, double[] posterior) {
		// yCounts += pOfYGivenX;
		DoubleArrays.addToSelf(yCounts, posterior);
		// xGivenYCounts += pOfYGivenX^T x instance
		instance.preMultiplyAndAddTo(posterior, xGivenYCounts);
	}

	/**
	 * Increments the counts for a single class for the given instance. Equivalent to calling updateStatistics(instance, double[]) 
	 * using an array with the yth element set to 1.0 (and all others at 0.0).
	 */
	public void incrementCounts(SparseFeatureVector instance, int y) {
		++yCounts[y];
		instance.addTo(xGivenYCounts[y]);
	}

	/**
	 * Returns the actual array used by the counter--even after reset. It is therefore advisable to clone the
	 * returned array immediately. 
	 */
	public double[] getYCounts() {
		return yCounts;
	}

	/**
	 * Returns the actual array used by the counter--even after reset. It is therefore advisable to deep clone the
	 * returned matrix immediately. 
	 */
	public double[][] getXGivenYCounts() {
		return xGivenYCounts;
	}
	
	@Override
	public PartialCounts clone() {
		return PartialCounts.newInstanceFromInitialCounts(yCounts, xGivenYCounts, true);
	}
	
	public static PartialCounts newInstanceWithZeroCounts(int numClusters, int numFeatures) {
		double[] initialYCounts = new double[numClusters];
		double[][] initialXGivenYCounts = new double[numClusters][numFeatures];

		return new PartialCounts(initialYCounts, initialXGivenYCounts);
	}
	
	public static PartialCounts newInstanceFromInitialCounts(double[] initialYCounts, double[][] initialXGivenYCounts,
			boolean copy) {
		if (copy) {
			initialYCounts = initialYCounts.clone();
			initialXGivenYCounts = Matrices.clone(initialXGivenYCounts);
		}
		
		return new PartialCounts(initialYCounts, initialXGivenYCounts);
	}

	/**
	 * Copies initial counts into specified arrays and uses arrays for counting.
	 */
	public static PartialCounts newInstanceFromInitialCounts(double[] yCounts, double[] initialYCounts,
			double[][] initialXGivenYCounts, double[][] xGivenYCounts) {
		System.arraycopy(initialYCounts, 0, yCounts, 0, initialYCounts.length);
		Matrices.copyInto(initialXGivenYCounts, xGivenYCounts);
		return newInstanceFromInitialCounts(yCounts, xGivenYCounts, false);
	}

	/**
	 * Creates a new object wherein each element of yCounts is initialized to alpha and each element of xGivenYCounts
	 * is initialized to beta.
	 */
	public static PartialCounts newInstanceWithConstantCounts(int numClusters, int numFeatures, double alpha,
			double beta) {
		double[] initialYCounts = DoubleArrays.constant(alpha, numClusters);
		double[][] initialXGivenYCounts = Matrices.constant(beta, numClusters, numFeatures);

		return new PartialCounts(initialYCounts, initialXGivenYCounts);
	}
	
}